using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Finance.DTO.FinanceUserAccount;
using AnQinFrameWorkAPI.Entity.Finance.Entity;
using AnQinFrameWorkAPI.Entity.Finance.View.FinanceUserAccount;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.Finance;

/// <summary>
///     财务用户账户业务层
/// </summary>
public class FinanceUserAccountService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //构造注入
    public FinanceUserAccountService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #region 获取财务用户账户列表

    public async Task<RequestResponseModel> GetFinanceUserAccountList(FinanceUserAccountSearchDTO financeUserAccountSearchDTO)
    {
        //筛选
        var query = _context.FinanceUserAccount
                .ProjectTo<FinanceUserAccountView>(_mapper.ConfigurationProvider)
                .AsNoTracking()
                .WhereIF(a => a.AccountName.Contains(financeUserAccountSearchDTO.AccountName),
                    !string.IsNullOrEmpty(financeUserAccountSearchDTO.AccountName))
                .WhereIF(a => a.CreateTime > financeUserAccountSearchDTO.StartTime, financeUserAccountSearchDTO.StartTime != null)
                .WhereIF(a => a.CreateTime < financeUserAccountSearchDTO.EndTime, financeUserAccountSearchDTO.EndTime != null)
            ;

        //返回列表
        var financeUserAccountList = await query
            .OrderBy(a => a.CreateTime)
            .Page(financeUserAccountSearchDTO.PageIndex, financeUserAccountSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<FinanceUserAccountView>(financeUserAccountList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增财务用户账户

    public async Task<RequestResponseModel> AddFinanceUserAccount(FinanceUserAccountAddDTO financeUserAccountAddDTO)
    {
        //生成新数据
        var financeUserAccount = _mapper.Map<FinanceUserAccount>(financeUserAccountAddDTO);

        financeUserAccount.Id = Guid.NewGuid();
        financeUserAccount.CreateTime = DateTime.Now;

        //新增数据
        _context.FinanceUserAccount.Add(financeUserAccount);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(financeUserAccount.Id, "新增成功");
    }

    #endregion

    #region 更新财务用户账户

    public async Task<RequestResponseModel> PutFinanceUserAccount(FinanceUserAccountUpdateDTO financeUserAccountUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.FinanceUserAccount, financeUserAccountUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("财务用户账户Id无效");

        //编辑数据
        var financeUserAccount = _mapper.Map(financeUserAccountUpdateDTO, query);

        //更新表
        _context.FinanceUserAccount.Update(financeUserAccount);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(financeUserAccountUpdateDTO.Id);
    }

    #endregion

    #region 删除财务用户账户

    public async Task<RequestResponseModel> DeleteFinanceUserAccount(SingleIdDTO deleteDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.FinanceUserAccount, deleteDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("财务用户账户Id无效");

        //删除数据
        _context.FinanceUserAccount.Remove(query);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion
}